from collections import deque


class SlidingTool:
    def __init__(self, name: str, maxlen: int):
        self.name = name
        self.maxlen = maxlen
        self.value = deque(maxlen=maxlen)
        self.s = deque(maxlen=maxlen)
        self.s2 = deque(maxlen=maxlen)

    def push(self, v):
        if len(self.s) < 1:
            self.s.append(v)
            self.s2.append(v**2)
        elif len(self.s) == self.maxlen:
            v0 = self.value.pop()
            self.s.append(self.s[-1] + v - v0)
            self.s2.append(self.s2[-1] + v**2 - v0**2)
        else:
            self.s.append(self.s[-1] + v)
            self.s2.append(self.s2[-1] + v**2)
        self.value.append(v)

    def get_value(self):
        if len(self.s) < self.maxlen:
            return None, None

        mean = self.s[-1] / self.maxlen
        delta = self.s2[-1] / self.maxlen - mean**2
        std = delta**0.5
        return mean, std


class SlidingData:
    def __init__(self, maxlen: int, periods: list):
        self.periods = periods
        self.maxlen = maxlen
        self.tools = {x: SlidingTool(f"ms{x}", x) for x in periods}
        self.queues = {x: deque(maxlen=maxlen) for x in periods}

    def push(self, value: float):
        for x, y in self.tools.items():
            y.push(value)
            m, s = y.get_value()
            self.queues[x].append([m, s])

    def get_value(self, period: int, idx: slice | int) -> list:
        return self.queues[period][idx]
